package org.example.v5.example;

import org.example.v5.example.enums.OrderEvent;
import org.example.v5.example.enums.OrderStateAction;
import org.example.v5.statemachine.StateAction;
import org.example.v5.statemachine.StateFactory;
import org.example.v5.statemachine.StateMachine;
import org.example.v5.example.action.PendingOrderStateAction;
import org.example.v5.example.action.PaidOrderStateAction;
import org.example.v5.example.action.ShippedOrderStateAction;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

public class StateMachineDemo {
    public static void main(String[] args) {
        // 创建状态映射
        Map<OrderStateAction, Supplier<StateAction<StateMachine<OrderStateAction, OrderEvent>, OrderEvent>>> stateMap = new HashMap<>();
        stateMap.put(OrderStateAction.PENDING, PendingOrderStateAction::new);
        stateMap.put(OrderStateAction.PAID, PaidOrderStateAction::new);
        stateMap.put(OrderStateAction.SHIPPED, ShippedOrderStateAction::new);

        // 创建状态工厂
        StateFactory<OrderStateAction, OrderEvent> stateFactory = new StateFactory<>(stateMap);
        // 创建状态机，初始状态为 PENDING
        StateMachine<OrderStateAction, OrderEvent> orderStateMachine = new StateMachine<>(stateFactory.getState(OrderStateAction.PENDING));

        // 启动状态机
        orderStateMachine.onEvent(OrderEvent.DELIVER); // 无效事件
        orderStateMachine.onEvent(OrderEvent.SHIP); // 无效事件
        orderStateMachine.onEvent(OrderEvent.PAY);

        orderStateMachine.onEvent(OrderEvent.PAY); // 尝试重复支付
    }
}
